#include "uiinterface.h"
#ifdef Q_OS_UNIX
#ifdef Q_OS_ANDROID
#include <QJniObject>
#include <QGuiApplication>
#include <QJniEnvironment>
#endif
#endif
#include <QSettings>

#include <QQmlApplicationEngine>

#include <QScreen>

#include "./sensor/sensor.h"
const char* darkTextColor= "#bababa";


void setKeepScreenOn(bool on) {
#ifdef Q_OS_UNIX
#ifdef Q_OS_ANDROID
    QJniObject activity = QNativeInterface::QAndroidApplication::context();
    if (activity.isValid()) {
        QJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
        if (window.isValid()) {
            const int FLAG_KEEP_SCREEN_ON = 128; // Android 的 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            if (on) {
                window.callMethod<void>("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
            } else {
                window.callMethod<void>("clearFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
            }
        }
    }
#endif
#endif
}
//#include
Q_GLOBAL_STATIC(UIInterface, uiinterface)
UIInterface::UIInterface(QObject *parent)
    : QObject{parent}
    ,textColor(darkTextColor)
  //  ,client(new TcpClient(this))
{
 //  qmlRegisterType<UIInterface>("UIModule", 1, 0, "UI");
    
    _InitSys();
   setKeepScreenOn(true);
    connect(SENSORINTERFACE,&Sensor::lightChanged,this,[this](){
       this->setLight(SENSORINTERFACE->lightLux());
   });
}

UIInterface *UIInterface::instance()
{
    return uiinterface;
}

void UIInterface::_InitSys()
{
    SENSORINTERFACE;
    Sensor::getALL();
    _setFullscreenLandscape();
   hideSecondEn = _getValue("hideSecond",hideSecondEn).toBool();
    showAPMEn = _getValue("showAPMEn",hideSecondEn).toBool();
   timeFormat = TimeFormat(_getValue("timeFormat",hideSecondEn).toInt());
}

void UIInterface::_setValue(const QString &key, const QVariant &value)
{
    QSettings settings("XQYR", "pm");

    settings.beginGroup("ScreenSaver");
    settings.setValue(key, value);

    settings.endGroup();
    qInfo()<<QString("set key:%1 value").arg(key)<<value;
}

QVariant UIInterface::_getValue(const QString &key,   QVariant defalut)
{
    QSettings settings("XQYR", "pm");
    settings.beginGroup("ScreenSaver");
    auto tmp =  settings.value(key,defalut);
    settings.endGroup();
    qInfo()<<QString("get key:%1 value").arg(key)<<tmp;
    return tmp;
}
/**
 * @brief UIInterface::_setFullscreenLandscape 隐藏状态栏和全屏
 */
void UIInterface::_setFullscreenLandscape()
{
#ifdef Q_OS_UNIX
#ifdef Q_OS_ANDROID
    QJniObject activity = QJniObject::callStaticObjectMethod(
        "org/qtproject/qt/android/QtNative",
        "activity",
        "()Landroid/app/Activity;"
        );

    if (activity.isValid()) {

    //     // 调用 Activity.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE)
    //     const int SCREEN_ORIENTATION_LANDSCAPE = 0; // Android 横屏标志
    //     activity.callMethod<void>(
    //         "setRequestedOrientation",
    //         "(I)V",
    //         SCREEN_ORIENTATION_LANDSCAPE
    //         );

    //     // 隐藏状态栏
        QJniObject window = activity.callObjectMethod(
            "getWindow",
            "()Landroid/view/Window;"
            );

        if (window.isValid()) {
            // Example: Set window flags
            const int FLAG_FULLSCREEN = 0x00000400;
            window.callMethod<void>(
                "addFlags",
                "(I)V",
                FLAG_FULLSCREEN
                );
        }

    }
#endif
#endif
}

double UIInterface::getLight() const
{
    return light;
}

void UIInterface::setLight(double newLight)
{
    if (qFuzzyCompare(light, newLight))
        return;
    light = newLight;
    emit lightChanged();
}

QString UIInterface::getTextColor() const
{
    return textColor;
}

void UIInterface::setTextColor(const QString &newTextColor)
{
    if (textColor == newTextColor)
        return;
    textColor = newTextColor;
    emit textColorChanged();
}

UIInterface::Theme UIInterface::getTheme() const
{
    return theme;
}

void UIInterface::setTheme(Theme newTheme)
{
    if (theme == newTheme)
        return;
    theme = newTheme;
    emit themeChanged();
}

bool UIInterface::getShowAPMEn() const
{
    return showAPMEn;
}

void UIInterface::setShowAPMEn(bool newShowAPMEn)
{
    if (showAPMEn == newShowAPMEn)
        return;

    showAPMEn = newShowAPMEn;
    _setValue("showAPMEn",showAPMEn);
    emit showAPMEnChanged();
}


UIInterface::TimeFormat UIInterface::getTimeFormat() const
{
    return timeFormat;
}

void UIInterface::setTimeFormat(TimeFormat newTimeFormat)
{
    if (timeFormat == newTimeFormat)
        return;
    timeFormat = newTimeFormat;
    _setValue("timeFormat",timeFormat);
    emit timeFormatChanged();
}

double UIInterface::getScale() const
{
    return scale;
}

void UIInterface::setScale(double newScale)
{
    if (qFuzzyCompare(scale, newScale))
        return;
    scale = newScale;
    emit scaleChanged();
}

bool UIInterface::getHideSecondEn() const
{
    return hideSecondEn;
}

void UIInterface::setHideSecondEn(bool newHideSecondEn)
{
    if (hideSecondEn == newHideSecondEn)
        return;

    hideSecondEn = newHideSecondEn;
    _setValue("hideSecond",hideSecondEn);
    emit hideSecondEnChanged();
}



